package com.weitu.occupationalHealth.monPoint.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.occupationalHealth.monPoint.entity.MonPoint;
import com.weitu.occupationalHealth.monPoint.service.MonPointService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * Created by WT-SUN on 2017/4/11.
 */
@Controller
@RequestMapping(value = "occupationalHealth/monPoint")
public class MonPointController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "occupationalHealth/monPoint/";
    
    @Autowired
    private MonPointService monPointService;

    /**
     * 跳转至监测点页面
     *
     * @return
     */
    @RequestMapping(value = "/list")
    public String list() {
        return MODULE_BATH_PATH + "list";
    }

    /**
     * 新增页面
     *
     * @return
     */
    @RequestMapping(value = "/add")
    public String add() {
        return MODULE_BATH_PATH + "add";
    }

    /**
     * 跳转详情与编辑页面
     *
     * @param id
     * @return
     */
    @RequestMapping("info")
    public ModelAndView info(String id, String type) {
        ModelAndView modelAndView = new ModelAndView();
        Map map=monPointService.selectById(id);
        modelAndView.addObject("mon", map);
        if (type.equals("info")) {
            modelAndView.setViewName(MODULE_BATH_PATH + "info");
        } else {
            modelAndView.setViewName(MODULE_BATH_PATH + "edit");
        }
        return modelAndView;
    }

    /**
     * 添加监测点
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("insertSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "职业卫生——监测点", methods = "添加监测点")
    public AjaxResult insertSelective() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getAddBasicFieldMap(paramMap);
        paramMap.put("delState",0);
        String pointId=paramMap.get("id").toString();
        int boo = monPointService.insertSelective(paramMap);
        int fint=0;
        if (paramMap.get("factorId")!=null){
            List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
            String conIdsp= paramMap.get("factorId").toString();
            String[] conIds=null;
            conIds=conIdsp.split(",");
            for (String conid:conIds){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("pointId",pointId);
                map.put("factorId",conid);
                map.put("delState",0);
                if (!list.contains(map)){
                    list.add(map);
                }
            }
            fint=monPointService.insertHarmIds(list);
        }
        if (boo > 0&&fint>0) {
            return success("添加成功");
        } else {
            return fail("添加失败");
        }
    }

    /**
     * 更新监测点
     * @return
     */
    @ResponseBody
    @RequestMapping("updateByIdSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "职业卫生——监测点", methods = "更新监测点")
    public AjaxResult updateByIdSelective() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getEditBasicFieldMap(paramMap);
        String pointId=paramMap.get("id").toString();
        Integer integer= monPointService.updateByIdSelective(paramMap);
        int fint=0;
        if (paramMap.get("factorId")!=null){
            List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
            String conIdsp= paramMap.get("factorId").toString();
            String[] conIds=null;
            conIds=conIdsp.split(",");
            for (String conid:conIds){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("pointId",pointId);
                map.put("factorId",conid);
                map.put("delState",0);
                list.add(map);
            }
            int dint=monPointService.deleteHarmIds(pointId);
            fint=monPointService.insertHarmIds(list);
        }
        if (integer > 0) {
            return success("更新成功");
        } else {
            return fail("更新失败");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteById")
    @Transactional(readOnly = false)
    @SystemLog(module = "职业卫生——监测点", methods = "删除监测点")
    public AjaxResult deleteById(String id){
        int boo=monPointService.deleteById(id);
        if (boo > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }

    /**
     * 监测点列表
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/pointTable", method = RequestMethod.POST)
    public Object pointTable(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        int totalNum=monPointService.selectMonPointCount(paramMap);
        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,totalNum);
        PageView pageView=dataGridPagination.getPageView();
        if (paramMap.get("sort") == null) {
            pageView.setSortColumn("addTime");
        }
        paramMap.put("pageView", pageView);
        List<Map> maps=monPointService.selectAllMonPoint(paramMap);
        Object data=dataGridPagination.flushDataGridData(maps);
        return data;
    }
    /**
     * 监测点列表 模块列表
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/selectPointTb", method = RequestMethod.POST)
    public Object selectPointTb(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        int totalNum=monPointService.selectPointTbCount(paramMap);
        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,totalNum);
        PageView pageView=dataGridPagination.getPageView();
        if (paramMap.get("sort") == null) {
            pageView.setSortColumn("addTime");
        }
        paramMap.put("pageView", pageView);
        List<MonPoint> maps=monPointService.selectPointTb(paramMap);
        Object data=dataGridPagination.flushDataGridData(maps);
        return data;
    }
    /**
     * 根据监测点ID查询关联危害因素
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectHarmIds", method = RequestMethod.POST)
    public List<Map<String,Object>> selectHarmIds(String id){
        return monPointService.selectHarmIds(id);
    }
}
